package com.asj.house.service.impl;

import cn.hutool.core.lang.Dict;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.asj.house.entity.HouseRoom;
import com.asj.house.entity.HouseRoomDetail;
import com.asj.house.mapper.HouseRoomMapper;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.service.HouseRoomDetailService;
import com.asj.house.service.HouseRoomService;
import com.asj.house.vo.result.CityRoomResult;
import com.asj.house.vo.result.RoomListResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 房屋信息表 服务实现类
 * </p>
 *
 * @author asj
 * @since 2021-03-04
 */
@Service
public class HouseRoomServiceImpl extends ServiceImpl<HouseRoomMapper, HouseRoom> implements HouseRoomService {

    @Autowired
    private HouseRoomDetailService houseRoomDetailService;

    @Override
    public RespEntity<RoomListResult> listHouseRoom(Integer pageNum, Integer pageSize) {
        QueryWrapper<HouseRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseRoom::getDeleted, 0);

        IPage<HouseRoom> iPage = new Page(pageNum, pageSize);
        IPage<HouseRoom> page = super.page(iPage, queryWrapper);

        //数据组装
        RoomListResult roomListResult = dataPackage(page);

        return new RespEntity<>(RespCode.SUCCESS, roomListResult);
    }

    @Override
    public RespEntity<RoomListResult> searchHouseRoom(Integer pageNum, Integer pageSize, String content) {
        QueryWrapper<HouseRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(HouseRoom::getName, content)
                .or().like(HouseRoom::getIntroduce, content)
                .or().like(HouseRoom::getPrice, content);

        IPage<HouseRoom> iPage = new Page<>(pageNum, pageSize);
        IPage<HouseRoom> page = super.page(iPage, queryWrapper);

        //数据组装
        RoomListResult roomListResult = dataPackage(page);

        return new RespEntity<>(RespCode.SUCCESS, roomListResult);
    }

    @Override
    public RespEntity<RoomListResult> typeHouseRoom(Integer pageNum, Integer pageSize, Integer type) {
        QueryWrapper<HouseRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseRoom::getType, type)
                .eq(HouseRoom::getDeleted, 0);

        IPage<HouseRoom> iPage = new Page<>(pageNum, pageSize);
        IPage<HouseRoom> page = super.page(iPage, queryWrapper);

        //数据组装
        RoomListResult roomListResult = dataPackage(page);

        return new RespEntity<>(RespCode.SUCCESS, roomListResult);
    }

    private RoomListResult dataPackage(IPage<HouseRoom> page) {
        RoomListResult roomListResult = new RoomListResult();
        roomListResult.setPageNum(page.getCurrent());
        roomListResult.setPageSize(page.getSize());
        roomListResult.setPageCount(page.getPages());
        roomListResult.setTotal(page.getTotal());
        roomListResult.setHouseRoomList(page.getRecords());
        return  roomListResult;
    }

    @Override
    public RespEntity<CityRoomResult> listHouseCity(String city) {
        CityRoomResult cityRoomResult = new CityRoomResult();

        QueryWrapper<HouseRoomDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseRoomDetail::getCity, city)
                .eq(HouseRoomDetail::getDeleted, 0);
        List<HouseRoomDetail> houseRoomDetailList = houseRoomDetailService.list(queryWrapper);

        if (Objects.isNull(houseRoomDetailList) || houseRoomDetailList.isEmpty()) {
            return new RespEntity<>(RespCode.SUCCESS, cityRoomResult);
        }

        List<Integer> roomIdList = houseRoomDetailList.stream()
                .map(HouseRoomDetail::getRoomId)
                .collect(Collectors.toList());

        List<HouseRoom> houseRoomList = baseMapper.selectBatchIds(roomIdList);
        cityRoomResult.setHouseRoomList(houseRoomList);

        return new RespEntity<>(RespCode.SUCCESS, cityRoomResult);
    }

    @Override
    public RespEntity<List<HouseRoom>> sameCityRoom(Integer roomId) {
        QueryWrapper<HouseRoomDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseRoomDetail::getRoomId, roomId)
                .eq(HouseRoomDetail::getDeleted, 0);
        HouseRoomDetail houseRoomDetail = houseRoomDetailService.getOne(queryWrapper);

        QueryWrapper<HouseRoomDetail> houseRoomDetailQueryWrapper = new QueryWrapper<>();
        houseRoomDetailQueryWrapper.lambda().eq(HouseRoomDetail::getCity, houseRoomDetail.getCity())
                .eq(HouseRoomDetail::getDeleted, 0);
        List<HouseRoomDetail> houseRoomDetailList = houseRoomDetailService.list(houseRoomDetailQueryWrapper);

        houseRoomDetailList = houseRoomDetailList.stream()
                .filter(v -> !roomId.equals(v.getRoomId()))
                .collect(Collectors.toList());

        if (houseRoomDetailList.isEmpty()) {
            return new RespEntity<>(RespCode.SUCCESS, new ArrayList<>());
        }

        List<Integer> roomIdList = houseRoomDetailList.stream()
                .map(HouseRoomDetail::getRoomId)
                .limit(4)
                .collect(Collectors.toList());

        List<HouseRoom> houseRoomList = baseMapper.selectBatchIds(roomIdList);

        return new RespEntity<>(RespCode.SUCCESS, houseRoomList);
    }

    @Override
    public RespEntity<RoomListResult> searchByDate(String begin, String end, Integer num) {
        QueryWrapper<HouseRoom> queryWrapper = new QueryWrapper<>();

        switch (num) {
            case 1 : queryWrapper.lambda().between(HouseRoom::getLive, 1, 2); break;
            case 2 : queryWrapper.lambda().between(HouseRoom::getLive, 2, 3); break;
            case 4 : queryWrapper.lambda().between(HouseRoom::getLive, 4, 5); break;
            case 6 : queryWrapper.lambda().between(HouseRoom::getLive, 6, 7); break;
            case 8 : queryWrapper.lambda().ge(HouseRoom::getLive, 8);
        }

        queryWrapper.lambda().le(HouseRoom::getOnTime, begin)
                .ge(HouseRoom::getOffTime, end);

        List<HouseRoom> houseRoomList = baseMapper.selectList(queryWrapper);

        return new RespEntity<>(RespCode.SUCCESS, new RoomListResult(houseRoomList));
    }

}
